﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using Model;
using BLL;
using Common;

namespace WebPortal.Controllers
{
    public class CodeMappingController : BaseController
    {
        public ActionResult List()
        {
            return View();
        }
        public ActionResult Edit(string id, string dbName)
        {
            if (id == null)//添加页面
                return View(new Model.CodeMapping());
            else//修改页面
            {
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                CodeMappingBll bll = new CodeMappingBll(loginInfos);
                CodeMapping model = bll.GetInfoById(id);
                return View(model);
            }
        }
        /// <summary>
        /// 映射列表
        /// </summary> 
        /// <param name="collection">页面数据</param>
        /// <returns></returns>
        public ActionResult GetList(string dbName,string name = "", int rows = 0, int page = 1)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            CodeMappingBll bll = new CodeMappingBll(loginInfos);
            IDataParameter[] paras = new SqlParameter[3];
            paras[0] = new SqlParameter("@Name", name);
            paras[1] = new SqlParameter("@Rows", rows);
            paras[2] = new SqlParameter("@PageNum", page);
            List<CodeMapping> list = bll.GetList(paras);//数据列表
            int count = 0;//总记录数
            if (list.Count > 0)
                count = Convert.ToInt32(list[0].Count);
            Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
            jsonDict.Add("total", count);
            jsonDict.Add("rows", list);

            return Json(jsonDict);
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public ActionResult SaveItem(FormCollection collection, string dbName)
        {
            Dictionary<string, string> query = collection["query"].FromJSon<Dictionary<string, string>>();
            try
            {
                SqlParameter[] paras = new SqlParameter[query.Count];
                int index = 0;
                foreach (string item in query.Keys)//解析提交数据
                {
                    paras[index] = new SqlParameter("@" + item, query[item]);
                    index += 1;
                }

                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                CodeMappingBll bll = new CodeMappingBll(loginInfos); 
                int r = bll.Update(paras); //保存信息
                return Json(r);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id, string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            CodeMappingBll bll = new CodeMappingBll(loginInfos);
            return Json(bll.Delete(id));
        }
        public ActionResult GetMapping(string dbName)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            CodeMappingBll bll = new CodeMappingBll(loginInfos);
            return Json(bll.GetMapping());
        }

    }
}
